import {ActionType, PageContainer, ProColumns, ProTable} from "@ant-design/pro-components";
import React, {useRef} from "react";
import {globalApi, globalLoadTableApiCode} from "@/services/admin-api/globalApi";
import {productSelect, storeSelect} from "@/pages/Inventory/Management";
import {useAccess} from "umi";

export type ProductInventoryRecord = {
  productInventoryRecordId: number
  productSpecificationId: number
  productSpecification: string
  productId: number
  productName: string
  productPrice: number
  productTotalPrice: number
  operateType: number
  storeId: number
  storeName: string
  afterInventory: number
  changeInventory: number
  beforeInventory: number
  changeType: number
  orderId: number
  remark: string
  delFlag: boolean
  createTime: string
  createUserId: number
  createUserName: string
  updateTime: string
  updateUserId: number
  updateUserName: string
}

export const changeType = await globalApi('/config/dict', {key: 'change_type'})

export const operateType = [{label: '出库', value: 0}, {label: '入库', value: 1}]

export default () => {
  const access = useAccess()

  const actionRef = useRef<ActionType>();

  const columns: ProColumns<ProductInventoryRecord>[] = [
    {title: 'ID', dataIndex: 'productInventoryRecordId', valueType: 'digit', hideInSearch: true},
    {title: '库存ID', dataIndex: 'inventoryId', valueType: 'digit', hideInSearch: true},
    {
      title: '产品',
      dataIndex: 'productId',
      valueType: 'select',
      hideInTable: true,
      fieldProps: {options: productSelect.data, showSearch: true}
    },
    {title: '产品ID', dataIndex: 'productId', valueType: 'digit', hideInSearch: true},
    {title: '产品名称', dataIndex: 'productName', valueType: 'text', hideInSearch: true},
    {
      title: '规格',
      dataIndex: 'productSpecificationId',
      valueType: 'select',
      hideInTable: true,
      dependencies: ['productId'],
      request: async (param) => {
        console.log('param = ', param)
        if (param.productId) {
          const r = await globalApi('/product/specificationSelect', param)
          return r.data
        }
        return []
      },
      fieldProps: {showSearch: true,}
    },
    {title: '规格ID', dataIndex: 'productSpecificationId', valueType: 'digit', hideInSearch: true},
    {title: '规格', dataIndex: 'productSpecification', valueType: 'text', hideInSearch: true},
    {
      title: '店铺',
      dataIndex: 'storeId',
      valueType: 'select',
      hideInTable: true,
      fieldProps: {options: storeSelect.data, showSearch: true}
    },
    {title: '店铺ID', dataIndex: 'storeId', valueType: 'digit', hideInSearch: true},
    {title: '店铺名称', dataIndex: 'storeName', valueType: 'text', hideInSearch: true},
    {title: '操作类型', dataIndex: 'operateType', valueType: 'select', fieldProps: {options: operateType}},
    {title: '变动类型', dataIndex: 'changeType', valueType: 'select', fieldProps: {options: changeType.data}},
    {title: '改变前', dataIndex: 'beforeInventory', valueType: 'digit', hideInSearch: true},
    {title: '变动库存', dataIndex: 'changeInventory', valueType: 'digit', hideInSearch: true},
    {title: '最新库存', dataIndex: 'afterInventory', valueType: 'digit', hideInSearch: true},
    // {title: '产品价格', dataIndex: 'productPrice', valueType: 'money', hideInSearch: true},
    // {title: '产品总价', dataIndex: 'productTotalPrice', valueType: 'money', hideInSearch: true},
    {title: '订单ID', dataIndex: 'orderId', valueType: 'text', hideInSearch: true},
    {title: '备注', dataIndex: 'remark', valueType: 'text', hideInSearch: true},
    {title: '创建时间', dataIndex: 'createTime', valueType: 'dateTime', hideInSearch: true,},
    {title: '创建用户ID', dataIndex: 'createUserId', valueType: 'digit', hideInSearch: true,},
    {title: '创建用户', dataIndex: 'createUserName', valueType: 'text', hideInSearch: true,},
    {title: '更新时间', dataIndex: 'updateTime', valueType: 'dateTime', hideInSearch: true,},
    {title: '更新用户ID', dataIndex: 'updateUserId', valueType: 'digit', hideInSearch: true,},
    {title: '更新用户', dataIndex: 'updateUserName', valueType: 'text', hideInSearch: true,},
  ];

  return <PageContainer header={{title: '', breadcrumb: {},}}>
    <ProTable<ProductInventoryRecord>
      columns={columns} actionRef={actionRef}
      search={{span: 4}}
      request={async (param) => globalLoadTableApiCode('/product/inventoryRecordTable', 'inventoryLog:list:search', access,param)}
      rowKey={(record) => record.productInventoryRecordId}
      columnsState={{
        defaultValue: {
          // productInventoryRecordId: {show: false},
          inventoryId: {show: false},
          productId: {show: false},
          productSpecificationId: {show: false},
          storeId: {show: false},
          delFlag: {show: false},
          createUserId: {show: false},
          updateUserId: {show: false}
        }
      }}/>

  </PageContainer>
}
